package net.qhzw.vip.qiyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.dto.ProductDto;
import net.qhzw.vip.qiyu.dto.UserCollectionDto;
import net.qhzw.vip.qiyu.entity.Product;
import net.qhzw.vip.qiyu.entity.UserCollection;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.ProductService;
import net.qhzw.vip.qiyu.service.UserCollectionService;
import net.qhzw.vip.qiyu.util.PageParam;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;

@RestController
@RequestMapping("/p/user/collection")
@Api(tags = "收藏接口")
public class UserCollectionController {

    @Autowired
    private UserCollectionService userCollectionService;

    @Autowired
    private ProductService productService;


    @GetMapping("/page")
    @ApiOperation(value = "分页返回收藏数据", notes = "根据用户id获取")
    public JsonResult<IPage<UserCollectionDto>> getUserCollectionDtoPageByUserId(PageParam page) {
        Long userId = SecurityUtils.getUserId();
        IPage<UserCollectionDto> userCollectionDtoPage = userCollectionService.getUserCollectionDtoPageByUserId(page, userId);
        return JsonResult.ok(userCollectionDtoPage);
    }

    @GetMapping("isCollection")
    @ApiOperation(value = "根据商品id获取该商品是否在收藏夹中", notes = "传入收藏商品id")
    public JsonResult isCollection(Long prodId) {
        if (productService.count(new LambdaQueryWrapper<Product>()
                .eq(Product::getProdId, prodId)) < 1) {
            throw new ApiException("该商品不存在");
        }
        Long userId = SecurityUtils.getUserId();
        boolean isCollection = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, userId)) > 0;
        return JsonResult.ok(isCollection);
    }

    @PostMapping("/addOrCancel/{prodId}")
    @ApiOperation(value = "添加/取消收藏", notes = "传入收藏商品id,如果商品未收藏则收藏商品，已收藏则取消收藏")
    @ApiImplicitParam(name = "prodId", value = "商品id", required = true, dataType = "Long")
    public JsonResult addOrCancel(@PathVariable Long prodId) {
        if (Objects.isNull(productService.getProductByProdId(prodId))) {
            throw new ApiException("该商品不存在");
        }
        Long userId = SecurityUtils.getUserId();
        if (userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getProdId, prodId)
                .eq(UserCollection::getUserId, userId)) > 0) {
            userCollectionService.remove(new LambdaQueryWrapper<UserCollection>()
                    .eq(UserCollection::getProdId, prodId)
                    .eq(UserCollection::getUserId, userId));
        } else {
            UserCollection userCollection = new UserCollection();
            userCollection.setCreateTime(new Date());
            userCollection.setUserId(userId);
            userCollection.setProdId(prodId);
            userCollectionService.save(userCollection);
        }
        return JsonResult.ok();
    }

    /**
     * 查询用户收藏商品数量
     */
    @GetMapping("count")
    @ApiOperation(value = "查询用户收藏商品数量", notes = "查询用户收藏商品数量")
    public JsonResult findUserCollectionCount() {
        Long userId = SecurityUtils.getUserId();
        int count = userCollectionService.count(new LambdaQueryWrapper<UserCollection>()
                .eq(UserCollection::getUserId, userId));
        return JsonResult.ok(count);
    }

    @GetMapping("/prods")
    @ApiOperation(value = "获取用户收藏商品列表", notes = "获取用户收藏商品列表")
    public JsonResult<IPage<ProductDto>> collectionProds(PageParam page) {
        Long userId = SecurityUtils.getUserId();
        IPage<ProductDto> productDtoIPage = productService.collectionProds(page, userId);
        return JsonResult.ok(productDtoIPage);
    }


}
